home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 485 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. From: Roly Perera <rolyp@private.nethead.co.uk>
  2. Message-ID: <3122614B.520C@private.nethead.co.uk>
  3. X-Original-Date: Wed, 14 Feb 1996 22:25:15 +0000
  4. Path: in2.uu.net!bounce-back
  5. Date: 15 Feb 96 11:59:55 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: deriving from a typedef name
  9. Organization: Gnosis
  10. References: <4fdinq$8ds@cnn.Princeton.EDU>
  11. X-Mailer: Mozilla 2.0 (Win95; I)
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMSMhAeEDnX0m9pzZAQE4xQF/VGUezVx7nL8hH1AQx5khx4obhi4Esf+h
  14.     QkgMiZBf827v+fIfv/np3+c4hOAzYfwD
  15.     =Kp/5
  16.  
  17. tim@franck.Princeton.EDU (Tim Hollebeek) wrote:
  18.  
  19. > I suspect this is correct behavior, but it sure is confusing, so I
  20. > thought I'd double check:
  21. >
  22. > template <class T>
  23. > class nVector {
  24. > };
  25. >
  26. > typedef nVector<double> Vector; // workaround for compiler that
  27. >                                 // doesn't handle <class T = double> > 
  28. >
  29. > class Point : public Vector { // derive from typedef name
  30. > public:
  31. >    Point(const Vector& v) { Vector::Vector(v); } // ***
  32. > };
  33. >
  34. > At ***, I get: test.C:9: no method `nVector<double>::Vector'
  35. > for obvious reasons.  However, it's not so obvious if you are just
  36. > looking at the declaration of class Point, and the alternatives:
  37.  
  38. The reason that 'Vector::Vector(v)' doesn't work is not that Vector is a 
  39. typedef.  In fact for most (all?) purposes, the typedef name is as good 
  40. as a class.  The problem is that the way to explicitly call a 
  41. constructor is by using the name of the constructor *unqualified* by the 
  42. scope of the class.  It's as though the constructor were defined in 
  43. namespace enclosing the class, rather than that of the class itself.  So 
  44. for a normal member function, you call it by qualifying it by the class 
  45. name (Vector::insert), but for a constructor, you refer to it just by 
  46. its name (Vector).  Your example would work with:
  47.     Point (const Vector& v) { Vector(v); }
  48. although this will just create a temporary variable, initialise it, and 
  49. then discard it (as your original code would do too, if it worked).
  50.  
  51. > Vector::nVector<double>(v); // parse error
  52. > nVector<double>::nVector<double>(v); // parse error
  53.  
  54. Same applies.  Simply 'nVector<double>(v);' would be sufficient.  Again, 
  55. such code is pointless unless nVector<double>'s copy constructor has 
  56. side effects.
  57. ---
  58. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  59.   Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  60.   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  61.